﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Data.EntityClient;
using System.ComponentModel;

namespace BioBooking.Data.CtrLag
{
    public class MovieController
    {
        BioBookingContainer container;

        public MovieController()
        {
            container = new BioBookingContainer();
        }

        public void InsertMovie(string titel, DateTime releaseDate, string instructor, string actors, string originalTitle, string country, string language, string description, Int32 runtime, Genre genre)
        {
            Movie m = new Movie();
            m.Titel = titel;
            m.ReleaseDate = releaseDate;
            m.Instructor = instructor;
            m.Actors = actors;
            m.OriginalTitle = originalTitle;
            m.Country = country;
            m.Language = language;
            m.Description = description;
            m.Runtime = runtime;
            m.Genre = genre;

            insertMovie(m);
        }

        public void insertMovie(Movie movie)
        {
            container.MovieSet.AddObject(movie);
            container.SaveChanges();
        }

        public void UpdateMovie(int key, string titel, DateTime releaseDate, string instructor, string actors, string originalTitle, string country, string language, string description, Int32 runtime, Genre genre)
        {
            Movie m = container.MovieSet.SingleOrDefault(p => p.Id == key);
            if (m == null)
                return;

            m.Titel = titel;
            m.ReleaseDate = releaseDate;
            m.Instructor = instructor;
            m.Actors = actors;
            m.OriginalTitle = originalTitle;
            m.Country = country;
            m.Language = language;
            m.Description = description;
            m.Runtime = runtime;
            m.Genre = genre;

            container.SaveChanges();
        }

        public void UpdateMovieObject(Movie movie)
        {
            if (movie.EntityState == EntityState.Detached)
                container.Attach(movie);

            container.SaveChanges();
        }

        public void DeleteMovie(Movie movie)
        {
            container.MovieSet.DeleteObject(movie);
            container.SaveChanges();
        }

        public List<Movie> SearchMovie(string movie)
        {
            return container.MovieSet
                .Where(p => p.Titel.Contains(movie))
                .ToList<Movie>();
        }

        public List<Movie> listAllMovies()
        {
            return container.MovieSet.Where(p => p.isActive == true).ToList<Movie>();
        }

        public List<Movie> getMoviesByDate(DateTime date)
        {
            IQueryable<Movie> movies = container.MovieSet.Where(p => p.Showings.Any(d=>d.Date == date)).Distinct();
            return movies.ToList<Movie>();
        }

        public Movie SearchMovie(int movieID)
        {
            return container.MovieSet.Where(p => p.Id == movieID).SingleOrDefault();
        }

        public List<Genre> GetAllGenres()
        {
            return container.GenreSet.ToList<Genre>();
        }
    }
}
